package com.wd.live.view.fragment

import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.tabs.TabLayout
import com.wd.live.model.base.BaseFragment
import com.wd.live.databinding.FragmentGSquareBinding
import com.wd.live.model.bean.TypeBean
import com.wd.live.model.tool.LoginHelper
import com.wd.live.view.adapter.LiveRecyAdapter1
import com.wd.live.viewmodel.ListViewModel

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 作者：马海钊
 * 时间：2023年8月18日16:00:52
 * 功能：广场
 */
class GSquareFragment : BaseFragment<ListViewModel, FragmentGSquareBinding>() {
    private var list: MutableList<TypeBean.Result>? = null
    private var param1: String? = null
    private var param2: String? = null
    private var adapter: LiveRecyAdapter1? = null
    override fun initData() {
        viewModel.getType()
        viewModel.getLive(100, 1, 14)

        viewModel.typeMutableLiveData.observe(this, Observer {
            it.result.forEach {
                binding.guangboTabid.addTab(
                    binding.guangboTabid.newTab().setText(it.name)
                )
            }
            binding.guangboTabid.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    if (tab != null) {
                        viewModel.getLive(it.result[tab.position].id, 1, 14)
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {

                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }
            })
        })
        viewModel.liveMutableLiveData.observe(this, Observer {
            Toast.makeText(context, it.result[0].nickName, Toast.LENGTH_SHORT).show()
            adapter = context?.let { it1 ->
                LiveRecyAdapter1(it1, it.result)
            }

            binding.guangboRecyid.layoutManager = GridLayoutManager(context, 2)
            binding.guangboRecyid.adapter = adapter
        })

    }

    override fun getViewBinding() = FragmentGSquareBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment GSquareFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance() =
            GSquareFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}